//
//  DLRiskEvaluationQAView.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/12/7.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLRiskEvaluationQAView.h"
#import "DLRiskEvaluationModel.h"
#import "DLRiskEvaluationAnsModel.h"

@interface DLRiskEvaluationQAView()

@property (nonatomic, strong) UILabel *queLabel;



@end

@implementation DLRiskEvaluationQAView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self layoutSubControls];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
- (void) layoutSubControls {
    
    UILabel *queLabel = [UILabel new];
    queLabel.frame = CGRectMake(20, 20, self.width - 40, 40);
    queLabel.textColor = kDLNormalBlackColor;
    queLabel.font = kSystemFont(16);
    queLabel.textAlignment = NSTextAlignmentLeft;
    queLabel.numberOfLines = 0;
    queLabel.lineBreakMode = NSLineBreakByCharWrapping;
    queLabel.tag = 666;
    [self addSubview:queLabel];
    self.queLabel = queLabel;
}
- (void) setQaModel:(DLRiskEvaluationModel *)qaModel {
    _qaModel = qaModel;
    
    //重置所有控件
    
//    for (UIView *subViewAsButton in self.subviews) {
//        if (subViewAsButton.tag > 999) {
//            //按钮
//            UIButton *button = (UIButton *)subViewAsButton;
//            button.selected = NO;
//        }
//        if (subViewAsButton.tag > 99 && subViewAsButton.tag < 999) {
//            //label
//            UILabel *label = (UILabel *)subViewAsButton;
//            label.text = @"";
//        }
//    }
     for (UIView *subViewAsButton in self.subviews) {
         if (subViewAsButton.tag != 666) {
             [subViewAsButton removeFromSuperview];
         }
    }
    
    self.queLabel.text = [NSString stringWithFormat:@"%@.  %@", qaModel.queSortOrder, qaModel.queName];
    
    NSArray *ansArr = qaModel.ansMutArr;
    if (ansArr.count > 0) {
        
        for (NSInteger i = 0; i < ansArr.count; i++) {
            
            DLRiskEvaluationAnsModel *model = ansArr[i];
            
            UIButton *ansButton = [UIButton buttonWithType:0];
            ansButton.frame = CGRectMake(self.queLabel.left, self.queLabel.bottom + 20 + (30 + 20) * i, 30, 30);
            [ansButton setImage:[UIImage imageNamed:@"risk_ans_normal"] forState:UIControlStateNormal];
            [ansButton setImage:[UIImage imageNamed:@"risk_ans_select"] forState:UIControlStateSelected];
            ansButton.tag = 1000 + i;
            [self addSubview:ansButton];
            [ansButton addTarget:self action:@selector(ansButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            
            UILabel *textLabel = [UILabel new];
            textLabel.frame = CGRectMake(ansButton.right, ansButton.top - 5, self.queLabel.width - ansButton.width, 40);
            textLabel.text = [NSString stringWithFormat:@"%@", model.ansName];
            textLabel.font = kSystemFont(13);
            textLabel.textColor = kColorRGB(102, 102, 102);
            textLabel.textAlignment = NSTextAlignmentLeft;
            textLabel.tag = 100 + model.ansId.integerValue;
            textLabel.numberOfLines = 0;
            textLabel.lineBreakMode = NSLineBreakByCharWrapping;
            [self addSubview:textLabel];
            textLabel.userInteractionEnabled = YES;
            textLabel.backgroundColor = [UIColor whiteColor];
            
            UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textLabelTap:)];
            [textLabel addGestureRecognizer:tapGes];
        }
        
    }
}
- (void) textLabelTap:(UITapGestureRecognizer *)tapGes {
    
    for (UIButton *ansButton in self.subviews) {
        if (ansButton.tag > 999) {
            ansButton.selected = NO;
        }
    }
    
    for (NSInteger i = 0; i < self.qaModel.ansMutArr.count; i++) {
        DLRiskEvaluationAnsModel *model = self.qaModel.ansMutArr[i];
        if (model.ansId.integerValue == (tapGes.view.tag - 100)) {
            UIButton *ansButton = [self viewWithTag:1000 + i];
            ansButton.selected = YES;
        }
    }
    
    if (self.chooseAnsBlock) {
        self.chooseAnsBlock(tapGes.view.tag - 100);
    }
}
- (void) ansButtonClick:(UIButton *)button {
    
    for (UIButton *ansButton in self.subviews) {
        if (ansButton.tag > 999) {
            ansButton.selected = NO;
        }
    }
    
    button.selected = YES;
    
    DLRiskEvaluationAnsModel *model = self.qaModel.ansMutArr[button.tag - 1000];
    
    if (self.chooseAnsBlock) {
        self.chooseAnsBlock(model.ansId.integerValue);
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
